package cn.socialcredits.activity.setting

import com.typesafe.config.{ConfigFactory, Config}
import scala.collection.JavaConverters._
import scala.util.Try

/**
 * 配置文件
 * Created by jingyang on 15/7/16.
 */
object Settings {
  val mongo = {
    val conf = ConfigFactory.load().getConfig("sc-activity.mongo")
    MongoSetting(
      getString(conf, "host", "localhost"),
      getInt(conf, "db-port", 27017),
      getString(conf, "db-name").get
    )
  }

  val email = {
    val c = ConfigFactory.load().getConfig("sc-activity.email")
    EmailSetting(
      getString(c, "host-name").get,
      getInt(c, "port-ssl").get,
      getString(c, "username").get,
      getString(c, "password").get,
      getString(c, "from").get,
      getStringList(c, "to")
    )
  }

  private def getStringList(conf: Config, name: String): Seq[String] = try {
    conf.getStringList(name).asScala
  } catch {
    case _: Exception =>
      Nil
  }

  private def getString(conf: Config, name: String, default: => String): String = getString(conf, name).getOrElse(default)

  private def getString(conf: Config, name: String): Try[String] = Try(conf.getString(name))

  private def getBoolean(conf: Config, name: String, default: => Boolean): Boolean =
    getBoolean(conf, name).getOrElse(default)

  private def getBoolean(conf: Config, name: String): Try[Boolean] = Try(conf.getBoolean(name))

  private def getInt(conf: Config, name: String, default: => Int): Int = getInt(conf, name).getOrElse(default)

  private def getInt(conf: Config, name: String): Try[Int] = Try(conf.getInt(name))
}

case class EmailSetting(hostName: String, portSsl: Int, username: String, password: String, from: String, to: Seq[String])

case class MongoSetting(host: String, dbPort: Int, dbName: String)
